package com.ytjj.qmyx.users.service.impl;

import cn.hutool.json.JSONUtil;
import com.ytjj.qmyx.users.mapper.UsersCardMapper;
import com.ytjj.qmyx.users.mapper.UsersMapper;
import com.ytjj.qmyx.users.model.UsersCard;
import com.ytjj.qmyx.users.service.WeChatReceiveService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.Map;

@Service
public class WeChatReceiveServiceImpl implements WeChatReceiveService {

    @Resource
    private UsersCardMapper wexinCardMapper;

    @Resource
    private UsersMapper usersMapper;


    @Override
    public int saveReceive(Map<String, String> notifyMap) {

        String toUserName = notifyMap.get("ToUserName");                            // 开发者微信号
        String fromUserName = notifyMap.get("FromUserName");                          // 领取方账号（OpenID）
        String msgType = notifyMap.get("MsgType");                               // 消息类型
        String event = notifyMap.get("Event");                                 // 事件类型
        String cardId = notifyMap.get("CardId");                                // 卡券id
        String userCardCode = notifyMap.get("UserCardCode");                          // code序列号
        String unionId = notifyMap.get("UnionId");                               // 领券用户的UnionId
        String outerStr = notifyMap.get("OuterStr");                               // 领券用户的UnionId

        UsersCard usersCard = new UsersCard();
        usersCard.setToUserName(toUserName);
        usersCard.setFromUserName(fromUserName);
        usersCard.setMsgType(msgType);
        usersCard.setEvent(event);
        usersCard.setCardId(cardId);
        usersCard.setUserCardCode(userCardCode);

        usersCard.setUserId(Integer.valueOf(outerStr));
        usersCard.setUnionId(unionId);
        usersCard.setCreateTime(new Date());
        usersCard.setResultJson(JSONUtil.toJsonStr(notifyMap));

        return  wexinCardMapper.insert(usersCard);

    }
}
